#include "uart.h"

#define tty_path "/dev/ttymxc2"

extern float pitch_data;    //俯仰角
extern int tx_pid_out;

void uart_init(void)
{

    int fd;

    fd = open(tty_path, O_RDWR);
    if( fd < 0 ){
        printf("uart dev open error!\n");
        close(fd);
        exit(-1);
    }
    set_uart(fd);
    // set_uart2(fd);

    char str[100] = {0};
    float tx_pitch = 0.0;

    while(1){
        tx_pitch = pitch_data;
        if( tx_pitch > 180.0 ){
            tx_pitch = tx_pitch-360.0;
        }
        sprintf(str, "<any>:%f, %f, %d\n",tx_pitch,0.0,tx_pid_out);
        write(fd, str, strlen(str));
        usleep(50*1000);
    }
    close(fd);

}

void set_uart(int fd)
{
    struct termios opt;
    tcflush(fd, TCIOFLUSH);
    tcgetattr(fd, &opt);
    
    opt.c_iflag &= (~CBAUD);
    opt.c_iflag &= (PARENB);
    opt.c_iflag &= (~ICRNL);
    opt.c_lflag &= (~ECHO);

    cfsetspeed(&opt, B115200);
    opt.c_cflag |= CS8;
    opt.c_cflag &= (~PARENB);
    opt.c_cflag &= (~CSTOPB);

    tcsetattr(fd, TCSANOW, &opt);
}

// void set_uart2(int fd)
// {
//     struct termios opt;
//     tcflush(fd, TCIFLUSH);

//     tcgetattr(fd, &opt);

//     cfsetispeed(&opt,B115200);
//     cfsetospeed(&opt,B115200);

//     opt.c_iflag |= ICRNL;

//     opt.c_cflag &= ~CSIZE;
//     opt.c_cflag |= CS8;

//     opt.c_cflag &= ~PARENB;
//     opt.c_iflag &= ~INPCK;
    
//     opt.c_cflag &= ~CSTOPB;

//     tcsetattr(fd, TCSANOW, &opt);
// }